<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Goods as GoodsM;
use think\Cache;
use think\Db;

/**
 * 盲盒接口
 */
class Goods extends Api
{
    /**
     * 盲盒统一售价
     * 价格都一样，随便取一个
    */
    public function box_price()
    {
        $price = model('goods')->value('price');
        $this->success('返回成功', $price);
    }

    /**
     * 盲盒列表
     */
    public function box_list()
    {
        $goods_id = input('goods_id', 0, 'intval');
        $list = GoodsM::goods_list($goods_id);
        $this->success('返回成功', $list);
    }

    /**
     * 开盲盒
    */
    public function draw_lottery()
    {
        $res = GoodsM::draw_lottery($this->auth->id);
        if ($res['error'] > 0) {
            $this->error($res['msg']);
        } else {
            $this->success('开盒成功', $res['data']);
        }
    }

    /**
     * 开到的盲盒列表
     * 参与合成用的五虎上将
    */
    public function lottery_list()
    {
        $list = Db::name('goods')->where(['is_compose' => 1])->select();
        $list2 = [];
        foreach ($list as $goods) {
            $order_count = Db::name('goods_order')->where(['uid' => $this->auth->id, 'goods_id' => $goods['id']])->value('num');
            $list2[] = [
                'id' => $goods['id'],
                'name' => $goods['name'],
                'order_count' => $order_count ?: 0,
                'image' => $goods['image']
            ];
        }
        $this->success('返回成功', $list2);
    }

    /**
     * 盲盒合成
    */
    public function compose()
    {
        $ids = input('ids', '', 'trim');
        $ids = json_decode($ids, true);
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        $res = GoodsM::compose($this->auth->id, $ids);
        if ($res['error'] > 0) {
            return $this->error($res['msg']);
        } else {
            $this->success('合成成功', $res['data']);
        }
    }

    /**
     * 合成后的奖品
    */
    public function compose_goods()
    {
        $id = input('id', 0, 'intval');
        $list = GoodsM::prize_list($id);
        $this->success('返回成功', $list);
    }
}
